package com.c.controller;

import com.c.pojo.entity.Account;
import com.c.pojo.entity.Answer;
import com.c.pojo.vo.AnswerVo;
import com.c.service.AnswerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

import javax.annotation.Resource;

/**
 * @author bysun
 */
@Controller
public class AnswerController {

    Logger logger = LoggerFactory.getLogger(AnswerController.class);

    @Resource
    AnswerService answerService;

    @GetMapping("/gotoAnswer/{id}")
    public String gotoAnswer(@PathVariable("id") Integer id, Model model) {
        logger.info("id:{}", id);
        Answer answer = answerService.getAnswerByAccountId(id);
        if (answer == null) {
            answer = new Answer();
            Account account = new Account();
            account.setId(id);
            answer.setAccount(account);
        }
        model.addAttribute("answer", answer);
        return "answer";
    }

    @PostMapping("/answer")
    public String answer(AnswerVo answerVo) {
        logger.info("answer:{}", answerVo.toString());
        answerService.addOrUpdateAnswer(answerVo);
        return "redirect:/getAccounts";
    }
}
